# -*- coding: utf-8 -*-
import os
import subprocess
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QMessageBox, QLabel


class LauncherApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # 设置窗口标题
        self.setWindowTitle("Python 文件启动器")

        # 创建垂直布局
        layout = QVBoxLayout()

        # 创建按钮
        self.button1 = QPushButton("运行TCP/UDP客服端或服务端", self)
        self.button2 = QPushButton("运行FTP客户端", self)
        self.exit_button = QPushButton("退出", self)

        # 将按钮点击事件连接到槽函数
        self.button1.clicked.connect(lambda: self.run_file("src/TcpUdpClient.py"))
        self.button2.clicked.connect(lambda: self.run_file("src/mainFTP.py"))

        self.exit_button.clicked.connect(self.close)

        # 将按钮添加到布局中
        layout.addWidget(self.button1)
        layout.addWidget(self.button2)

        layout.addWidget(self.exit_button)

        # 设置窗口布局
        self.setLayout(layout)

    def run_file(self, file_path):
        if os.path.exists(file_path):
            try:
                subprocess.run(["python", file_path], check=True)
                QMessageBox.information(self, "成功", f"已运行 {file_path}")
            except subprocess.CalledProcessError as e:
                QMessageBox.critical(self, "错误", f"运行 {file_path} 时出错：{e}")
        else:
            QMessageBox.warning(self, "错误", f"文件 {file_path} 不存在！")


if __name__ == "__main__":
    # 创建应用程序对象
    app = QApplication([])

    # 创建主窗口
    window = LauncherApp()
    window.show()

    # 运行应用程序
    app.exec()